Expand description
Easily export your Rust types to other languages
Specta provides a system for type introspection and a set of language exporters which allow you to export your Rust types to other languages!
Currently we only support exporting to TypeScript but work has begun on other languages.
Features
- Export structs and enums to Typescript
- Get function types to use in libraries like tauri-specta
- Supports wide range of common crates in Rust ecosystem
- Supports type inference - can determine type of
fn demo() -> impl Type
.
Ecosystem
Specta can be used in your application either directly or through a library which simplifies the process of using it.
- rspc for easily building end-to-end typesafe APIs
- tauri-specta for typesafe Tauri commands
Example
use specta::{*, ts::*};
#[derive(Type)]
pub struct MyCustomType {
pub my_field: String,
}
fn main() {
assert_eq!(
ts::export::<MyCustomType>(&ExportConfiguration::default()).unwrap(),
"export type MyCustomType = { my_field: string }".to_string()
);
}
Supported Libraries
If you are using Prisma Client Rust you can enable the rspc
feature on it to allow for Specta support on types coming directly from your database. This includes support for the types created via a selection.
Feature flags
functions
— Support for exporting the types of Rust functions.
Compatability
-
serde
(enabled by default) — Support for serde and serde-json -
tauri
— Support for Tauri. This is required when usingspecta::function
with Tauri Commands.
Languages
typescript
(enabled by default) — Support for TypeScript language exporting
External types
-
uuid
— uuid crate -
chrono
— chrono crate -
time
— time crate -
bigdecimal
— bigdecimal crate -
rust_decimal
— rust_decimal crate -
indexmap
— indexmap crate -
ipnetwork
— ipnetwork crate -
mac_address
— mac_address crate -
bit-vec
— bit-vec crate -
bson
— bson crate -
uhlc
— uhlc crate -
bytesize
— bytesize crate -
glam
— glam crate -
tokio
— tokio crate -
url
— url crate
Alternatives
Why not ts-rs?
ts-rs is a great library, but it has a few limitations which became a problem when I was building rspc. Namely it deals with types individually which means it is not possible to export a type and all of the other types it depends on.
Why not Typeshare?
Typeshare is also great, but its approach is fundamentally different. While Specta uses traits and runtime information, Typeshare statically analyzes your Rust files. This results in a loss of information and lack of compatability with types from other crates.
Modules
- Types related to working with
DataType
. Exposed for advanced users. - export
export
Provides the global type store and a method to export them to other languages. - functions
functions
Support for exporting Rust functions. - ts
typescript
TypeScript language exporter. - Contains
Type
and everything related to it, including implementations and helper macros
Macros
- collect_types
functions
- fn_datatype
functions
Returns aFunctionDataType
for a given function that has been annotated withspecta
.
Attribute Macros
- specta
functions
Prepares a function to have its types extracted usingfn_datatype